﻿using System;
using System.Numerics;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;

namespace Mrkz.ImageManipulation
{  
    [Obsolete]
    public static class ComplexImageFactory
    {
        public static ComplexImage CreateEmptyImage(int Height, int Width)
        {
            ComplexImage e = new ComplexImage();

            e._data = new ComplexPixel[Height, Width];
            e._height = Height;
            e._width = Width;
            e._cropArea = new Int32Rect(0, 0, Width, Height);

            return e;
        }
        public static ComplexImage CreateEmptyImage(int Height, int Width, Int32Rect cropArea)
        {
            ComplexImage e = new ComplexImage();

            e._cropArea = cropArea;
            e._data = new ComplexPixel[Height, Width];
            e._height = Height;
            e._width = Width;

            return e;
        }

        public static ComplexImage FromImage(Image src)
        {
            ComplexImage e = new ComplexImage();

            e._width = src.Width;
            e._height = src.Height;
            e._data = new ComplexPixel[e._height, e._width];
            e._cropArea = src.CropArea;

            for (int i = 0; i < e._height; i++)
                for (int j = 0; j < e._width; j++)
                {
                    e._data[i, j] = new ComplexPixel(new Complex(src.GetPixel(i, j).Red, 0),
                                                new Complex(src.GetPixel(i, j).Green, 0),
                                                new Complex(src.GetPixel(i, j).Blue, 0));
                }

            return e;
        }

        public static ComplexImage CreateEmptyImage(ComplexImage Model)
        {
            ComplexImage e = new ComplexImage();

            e._data = new ComplexPixel[Model._height, Model._width];
            e._height = Model._height;
            e._width = Model._width;
            e._cropArea = Model.CropArea;

            return e;
        }
        public static ComplexImage CreateEmptyImage(Image Model)
        {
            ComplexImage e = new ComplexImage();

            e._data = new ComplexPixel[Model.Height, Model.Width];
            e._height = Model.Height;
            e._width = Model.Width;
            e._cropArea = Model.CropArea;

            return e;
        }

        public static ComplexImage FromBitmapSource(BitmapSource src)
        {
            ComplexImage e = new ComplexImage();

            e._width = src.PixelWidth;
            e._height = src.PixelHeight;
            e._data = CopyPixels(src);
            e._cropArea = new Int32Rect(0, 0, e._width, e._height);

            return e;
        }

        /// <summary>
        /// Create an image from existing data
        /// </summary>
        /// <param name="Height"></param>
        /// <param name="Width"></param>
        /// <param name="CropArea"></param>
        /// <param name="R"></param>
        /// <param name="G"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        public static ComplexImage FromComplexPixelArray(int Height, int Width, Int32Rect CropArea, Complex[,] R, Complex[,] G, Complex[,] B)
        {
            ComplexImage image = ComplexImageFactory.CreateEmptyImage(Height, Width, CropArea);

            Parallel.For(0, Height, (y) =>
            {
                for (int x = 0; x < Width; x++)
                    image.SetPixelWithPad(y, x,
                        new ComplexPixel(R[y, x], G[y, x], B[y, x]));
            });

            return image;
        }

        /// <summary>
        /// Import an real image into a complex image as a real part
        /// </summary>
        /// <param name="SourceImage"></param>
        /// <returns></returns>
        static ComplexPixel[,] CopyPixels(BitmapSource SourceImage)
        {
            ComplexPixel[,] cpixels = new ComplexPixel[SourceImage.PixelHeight, SourceImage.PixelWidth];
            Pixel [,] pixels = ImageFactory.CopyPixels(SourceImage);
            
            for (int i = 0; i < SourceImage.PixelHeight; i++)
                for (int j = 0; j < SourceImage.PixelWidth; j++)
                    cpixels[i, j] = new ComplexPixel(pixels[i, j]);

            return cpixels;
        }       
    }
}
